//
//  GameDetailViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/26.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "GameDetailViewController.h"
//#import "OriginalPublishViewController.h"
#import "OriginalPublishVC.h"
#import "ShareView.h"

#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "WeiboSDK.h"
#import "WXApi.h"

@interface GameDetailViewController ()<TencentSessionDelegate>

@property (nonatomic, strong) UIView *publishView;

@property (nonatomic, strong) UIButton *shadowBtn;
@property (nonatomic, strong) ShareView *shareView;
@property (nonatomic, copy) NSString *proId;
@property (nonatomic, copy) NSString *shareId;
@property (nonatomic, copy) NSString *shareName;
@property (nonatomic, copy) NSString *shareLink;

@property (nonatomic, strong) TencentOAuth *tencentOAuth;

@end

@implementation GameDetailViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [kNotificationCenter addObserver:self selector:@selector(shareSuccess) name:@"WechatShare" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.shadowBtn removeFromSuperview];
    [self.shareView removeFromSuperview];
    [kNotificationCenter removeObserver:self name:@"WechatShare" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"比赛详情";
    [self setRightButton:ImageWithName(@"分享") title:nil target:self action:@selector(getShare)];
    
    UIView *allBg = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(24), SCREEN_WIDTH - ScreenX375(48), ScreenX375(472))];
    allBg.layer.cornerRadius = ScreenX375(8);
    allBg.layer.shadowColor = RGB_COLOR(116, 116, 116).CGColor;
    allBg.layer.shadowOpacity = 0.07;
    allBg.layer.shadowOffset = CGSizeMake(0, 4);
    allBg.layer.shadowRadius = 5;
    [self.view addSubview:allBg];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(24), SCREEN_WIDTH - ScreenX375(48), ScreenX375(472))];
    imageView.image = ImageWithName(@"大赛图片");
    imageView.layer.cornerRadius = 8;
    imageView.clipsToBounds = YES;
    [self.view addSubview:imageView];
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(32), ScreenX375(546), SCREEN_WIDTH - ScreenX375(64), ScreenX375(37))];
    [sureBtn setTitle:@"发布作品" forState:UIControlStateNormal];
    sureBtn.titleLabel.font = Kfont(15);
    [sureBtn setTitleColor:REXADECIMALCOLOR(@"#222222") forState:UIControlStateNormal];
    sureBtn.layer.cornerRadius = ScreenX375(18.5);
    sureBtn.backgroundColor = REXADECIMALCOLOR(@"#ffd100");
    [sureBtn addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
}

- (void)shareSuccess {
    NSDictionary *parameter = @{@"id":_shareId,@"userId":kNSUDefaultReadKey(@"userId")};
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/shareSuccess",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            [HttpManager showNoteMsg:@"分享成功"];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)getShare {
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/share",REQUESTHEADER] andParameter:@{@"proId":_proDic[@"id"],@"userId":kNSUDefaultReadKey(@"userId"),@"type":@"2"} success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            self.shareId = [NSString stringWithFormat:@"%@",successResponse[@"data"][@"id"]];
            self.shareLink = [NSString stringWithFormat:@"%@",successResponse[@"data"][@"url"]];
            self.shareName = self.proDic[@"title"];
            [self share];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)share {
    if (!self.shareView) {
        self.shadowBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.shadowBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        [self.shadowBtn addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
        self.shadowBtn.hidden = YES;
        [kKeyWindow addSubview:self.shadowBtn];
        
        self.shareView = [[ShareView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, ScreenX375(180))];
        __weak __typeof(self) weakSelf = self;
        self.shareView.cancel = ^{
            [weakSelf hidden];
        };
        self.shareView.wxClick = ^{
            SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
            sendReq.bText = NO;
            sendReq.scene = 0;
            
            // 2.创建分享内容
            WXMediaMessage *message = [WXMediaMessage message];
            //分享标题
            message.title = @"虾拼";
            // 描述
            message.description = weakSelf.shareName;
            //分享图片,使用SDK的setThumbImage方法可压缩图片大小
            //                message.thumbData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMAGEHEADER,self.model.pics.firstObject]]];
            [message setThumbImage:[UIImage imageNamed:@"虾拼 logo"]];
            
            //创建多媒体对象
            WXWebpageObject *webObj = [WXWebpageObject object];
            // 点击后的跳转链接
            webObj.webpageUrl = weakSelf.shareLink;
            message.mediaObject = webObj;
            sendReq.message = message;
            [WXApi sendReq:sendReq];
            [weakSelf hidden];
        };
        self.shareView.wxGroupClick = ^{
            SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
            sendReq.bText = NO;
            sendReq.scene = 1;
            
            // 2.创建分享内容
            WXMediaMessage *message = [WXMediaMessage message];
            //分享标题
            message.title = @"虾拼";
            // 描述
            message.description = weakSelf.shareName;
            //分享图片,使用SDK的setThumbImage方法可压缩图片大小
            [message setThumbImage:[UIImage imageNamed:@"虾拼 logo"]];
            
            //创建多媒体对象
            WXWebpageObject *webObj = [WXWebpageObject object];
            // 点击后的跳转链接
            webObj.webpageUrl = weakSelf.shareLink;
            message.mediaObject = webObj;
            sendReq.message = message;
            [WXApi sendReq:sendReq];
            [weakSelf hidden];
        };
        self.shareView.qqClick = ^{
            weakSelf.tencentOAuth = [[TencentOAuth alloc] initWithAppId:kQQAppKey andDelegate:weakSelf];
            
            NSString *url = weakSelf.shareLink;
            NSString *imageUrl = [NSString stringWithFormat:@"%@",IMAGEHEADER];
            
            QQApiNewsObject *share = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:@"虾拼" description:weakSelf.shareName previewImageURL:[NSURL URLWithString:imageUrl]];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:share];
            
            //分享到qq
            [QQApiInterface sendReq:req];
            [weakSelf hidden];
        };
        self.shareView.qqGroupClick = ^{
            weakSelf.tencentOAuth = [[TencentOAuth alloc] initWithAppId:kQQAppKey andDelegate:weakSelf];
            
            NSString *url = weakSelf.shareLink;
            NSString *imageUrl = [NSString stringWithFormat:@"%@",IMAGEHEADER];
            
            QQApiNewsObject *share = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:@"虾拼" description:weakSelf.shareName previewImageURL:[NSURL URLWithString:imageUrl]];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:share];
            
            //分享到qq
            [QQApiInterface SendReqToQZone:req];
            [weakSelf hidden];
        };
        self.shareView.wbClick = ^{
            //微博
            WBMessageObject *message = [WBMessageObject message];
            message.text = [NSString stringWithFormat:@"虾拼 %@  %@",weakSelf.shareName,weakSelf.shareLink];
            
            WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
            authRequest.redirectURI = kWeiboAppRedirectURL;
            authRequest.scope = @"all";
            
            WBSendMessageToWeiboRequest *sharerequest = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:authRequest access_token:nil];
            [WeiboSDK sendRequest:sharerequest];
            [weakSelf hidden];
        };
        [kKeyWindow addSubview:self.shareView];
    }
    self.shadowBtn.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.shareView.frame = CGRectMake(0, SCREEN_HEIGHT - ScreenX375(180), SCREEN_WIDTH, ScreenX375(180));
    }];
}

- (void)hidden {
    [UIView animateWithDuration:0.3 animations:^{
        self.shareView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, ScreenX375(180));
    } completion:^(BOOL finished) {
        self.shadowBtn.hidden = YES;
    }];
}

- (void)sure {
    OriginalPublishVC *vc = [[OriginalPublishVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)close {
    [_publishView removeFromSuperview];
}

- (void)publish {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
